﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace CMS_GYM
{
    public class clsKhuyenMaiDAO
    {
        /// <summary>
        /// Kết nối SQL Server
        /// </summary>
        public CSQLServer sqlServer = new CSQLServer();

        /// <summary>
        /// Lấy Mã mặt hàng kế tiếp để phục vụ cho chức năng Insert
        /// </summary>
        public string LayMaKhuyenMaiMoi()
        {
            //string sql = "sp_GetNewMaNhanVien";
            //return sqlServer.readData(sql, "@MaNhanVienMoi");
            return "";
        }

        public clsKhuyenMaiDTO LayThongTin(int KhuyenMaiId)
        {
            string sql = "sp_GetInfoKhuyenMai";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@KhuyenMaiId";
            valueofParameter[0] = KhuyenMaiId;
            DataTable table = sqlServer.readData(sql, ParameterColection, valueofParameter);
            if (table != null && table.Rows.Count > 0)
            {
                return ChuyenDoi(table.Rows[0]);
            }
            return null;
        }

        public List<clsKhuyenMaiDTO> LayDanhSachKhuyenMaiCuaMotKhachHang(decimal KhachHangId)
        {
            try
            {
                List<clsKhuyenMaiDTO> list = new List<clsKhuyenMaiDTO>();
                string sql = "spGetBangKhuyenMaiTheoKhachHangId";
                string[] ParameterColection = new string[1];
                Object[] valueofParameter = new Object[1];
                ParameterColection[0] = "@KhachHangId";
                valueofParameter[0] = KhachHangId;
                DataTable table = sqlServer.readData(sql, ParameterColection, valueofParameter);
                if (table != null && table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        clsKhuyenMaiDTO KM = ChuyenDoi(table.Rows[i]);
                        if (KM != null)
                        {
                            list.Add(KM);
                        }
                    }
                }
                return list;
            }
            catch(Exception ex)
            {
                return null;
            }
        }

        public DataTable LayBangKhuyenMaiCuaMotKhachHang(decimal KhachHangId)
        {
            try
            {
                string sql = "spGetBangKhuyenMaiTheoKhachHangId";
                string[] ParameterColection = new string[1];
                Object[] valueofParameter = new Object[1];
                ParameterColection[0] = "@KhachHangId";
                valueofParameter[0] = KhachHangId;
                return sqlServer.readData(sql, ParameterColection, valueofParameter);
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        private clsKhuyenMaiDTO ChuyenDoi( DataRow Dong)
        {
            clsKhuyenMaiDTO KhuyenMai = new clsKhuyenMaiDTO();
            if (Dong !=null)
            {
                KhuyenMai.KhuyenMaiId = int.Parse(Dong["KhuyenMaiId"].ToString());
                KhuyenMai.MaKhuyenMai = Dong["MaKhuyenMai"] == null ? string.Empty : Dong["MaKhuyenMai"].ToString();
                KhuyenMai.TenKhuyenMai = Dong["TenKhuyenMai"] == null ? string.Empty : Dong["TenKhuyenMai"].ToString();
                KhuyenMai.MoTa = Dong["MoTa"] == null ? string.Empty : Dong["MoTa"].ToString();
                KhuyenMai.NgayBatDau = Dong["NgayBatDau"]==null ||string.IsNullOrEmpty(Dong["NgayBatDau"].ToString())? new DateTime() : DateTime.Parse(Dong["NgayBatDau"].ToString());
                KhuyenMai.NgayKetThuc = Dong["NgayKetThuc"] == null || string.IsNullOrEmpty(Dong["NgayKetThuc"].ToString()) ? new DateTime() : DateTime.Parse(Dong["NgayKetThuc"].ToString());
                KhuyenMai.SoTien = Dong["SoTien"] == null || string.IsNullOrEmpty(Dong["SoTien"].ToString()) ? 0 : double.Parse(Dong["SoTien"].ToString());
                KhuyenMai.PhanTram = Dong["PhanTram"] == null || string.IsNullOrEmpty(Dong["PhanTram"].ToString()) ? 0 : int.Parse(Dong["PhanTram"].ToString());
                KhuyenMai.SoThuTu = Dong["SoThuTu"] == null || string.IsNullOrEmpty(Dong["SoThuTu"].ToString()) ? 0 : int.Parse(Dong["SoThuTu"].ToString());
                KhuyenMai.TrangThai = bool.Parse(Dong["TrangThai"].ToString()) == true ? 1 : 0;
                KhuyenMai.IsLock = bool.Parse(Dong["IsLock"].ToString()) == true ? 1 : 0;
                KhuyenMai.NguoiTaoId = Dong["NguoiTaoId"] == null || string.IsNullOrEmpty(Dong["NguoiTaoId"].ToString()) ? 0 : int.Parse(Dong["NguoiTaoId"].ToString());
                KhuyenMai.NgayTao = Dong["NgayTao"] == null || string.IsNullOrEmpty(Dong["NgayTao"].ToString()) ? DateTime.Now : DateTime.Parse(Dong["NgayTao"].ToString());
                KhuyenMai.NguoiChinhSuaId = Dong["NguoiChinhSuaId"] == null || string.IsNullOrEmpty(Dong["NguoiChinhSuaId"].ToString()) ? 0 : int.Parse(Dong["NguoiChinhSuaId"].ToString());
                KhuyenMai.NgayChinhSua = Dong["NgayChinhSua"] == null || string.IsNullOrEmpty(Dong["NgayChinhSua"].ToString()) ? DateTime.Now : DateTime.Parse(Dong["NgayChinhSua"].ToString());
                return KhuyenMai;
            }
            return null;
        }

        public DataTable LayBang()
        {
            string sql = "spGetBangKhuyenMai";
            DataTable table = sqlServer.readData(sql);
            return table;
        }

        public DataTable LayBangConApDung()
        {
            string sql = "spGetBangKhuyenMaiConApDung";
            DataTable table = sqlServer.readData(sql);
            return table;
        }

        public int Sua(clsKhuyenMaiDTO KhuyenMai)
        {
            int i = -1;
            string sql = "sp_UpdateKhuyenMai";
            string[] ParameterColection = new string[8];
            Object[] valueofParameter = new Object[8];
            ParameterColection[0] = "@KhuyenMaiId";
            valueofParameter[0] = KhuyenMai.KhuyenMaiId;
            ParameterColection[1] = "@MaKhuyenMai";
            valueofParameter[1] = KhuyenMai.MaKhuyenMai;
            ParameterColection[2] = "@TenKhuyenMai";
            valueofParameter[2] = KhuyenMai.TenKhuyenMai;
            ParameterColection[3] = "@MoTa";
            valueofParameter[3] = KhuyenMai.MoTa;
            ParameterColection[4] = "@NgayBatDau";
            valueofParameter[4] = KhuyenMai.NgayBatDau;
            ParameterColection[5] = "@NgayKetThuc";
            valueofParameter[5] = KhuyenMai.NgayKetThuc;
            ParameterColection[6] = "@SoTien";
            valueofParameter[6] = KhuyenMai.SoTien;
            ParameterColection[7] = "@PhanTram";
            valueofParameter[7] = KhuyenMai.PhanTram;

            
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        public int Them(clsKhuyenMaiDTO KhuyenMai)
        {
            int i = -1;
            string sql = "sp_InsertKhuyenMai";
            string[] ParameterColection = new string[7];
            Object[] valueofParameter = new Object[7];
            ParameterColection[0] = "@MaKhuyenMai";
            valueofParameter[0] = KhuyenMai.MaKhuyenMai;
            ParameterColection[1] = "@TenKhuyenMai";
            valueofParameter[1] = KhuyenMai.TenKhuyenMai;
            ParameterColection[2] = "@MoTa";
            valueofParameter[2] = KhuyenMai.MoTa;
            ParameterColection[3] = "@NgayBatDau";
            valueofParameter[3] = KhuyenMai.NgayBatDau;
            ParameterColection[4] = "@NgayKetThuc";
            valueofParameter[4] = KhuyenMai.NgayKetThuc;
            ParameterColection[5] = "@SoTien";
            valueofParameter[5] = KhuyenMai.SoTien;
            ParameterColection[6] = "@PhanTram";
            valueofParameter[6] = KhuyenMai.PhanTram;
            
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        public int Xoa(int KhuyenMaiId)
        {
            int i = -1;
            string sql = "sp_DeleteKhuyenMai";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@KhuyenMaiId";
            valueofParameter[0] = KhuyenMaiId;
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Lay gia khuyen mai dong 6 thang duoc huong 
        /// </summary>
        /// <returns></returns>
        public Double KhuyenMaiDongTruocSauThang()
        {
            string sql = "spGetInfoDongTruocSauThang";
            return Double.Parse(sqlServer.readData(sql, "@DongTruocSauThang"));
        }

        /// <summary>
        /// Lay gia khuyen mai dong 12 thang duoc huong 
        /// </summary>
        /// <returns></returns>
        public Double KhuyenMaiDongTruocMuoiHaiThang()
        {
            string sql = "spGetInfoDongTruocMuoiHaiThang";
            return Double.Parse(sqlServer.readData(sql, "@DongTruocMuoiHaiThang"));
        }

        /// <summary>
        /// Lay gia khuyen mai sinh nhat va dong 12 ky
        /// <returns></returns>
        public Double KhuyenMaiSinhNhatVaDongMuoiHaiKy()
        {
            string sql = "spGetInfoSinhNhatVaDongMuoiHaiKy";
            return Double.Parse(sqlServer.readData(sql, "@SinhNhatVaDongMuoiHaiKy"));
        }

        /// <summary>
        /// Cập nhật khuyen mai dong 6 thang duoc huong 
        /// </summary>
        public int CapNhatKhuyenMaiDongTruocSauThang(Double DongTruocSauThang)
        {
            int i = -1;
            string sql = "spUpdateDongTruocSauThang";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@DongTruocSauThang";
            valueofParameter[0] = DongTruocSauThang;
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Cập nhật khuyen mai dong 12 thang duoc huong
        /// </summary>
        public int CapNhatKhuyenMaiDongTruocMuoiHaiThang(Double DongTruocMuoiHaiThang)
        {
            int i = -1;
            string sql = "spGetInfoDongTruocMuoiHaiThang";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@DongTruocMuoiHaiThang";
            valueofParameter[0] = DongTruocMuoiHaiThang;
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }

        /// <summary>
        /// Cập nhật khuyen mai sinh nhat va dong 12 ky
        /// </summary>
        public int CapNhatKhuyenMaiSinhNhatVaDongMuoiHaiKy(Double SinhNhatVaDongMuoiHaiKy)
        {
            int i = -1;
            string sql = "spUpdateSinhNhatVaDongMuoiHaiKy";
            string[] ParameterColection = new string[1];
            Object[] valueofParameter = new Object[1];
            ParameterColection[0] = "@SinhNhatVaDongMuoiHaiKy";
            valueofParameter[0] = SinhNhatVaDongMuoiHaiKy;
            i = sqlServer.writeData(sql, ParameterColection, valueofParameter);
            return i;
        }


    }
}
